package com.show.business.integrate.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.integrate.bean.DyvmConfig;
import com.show.business.integrate.mapper.DyvmConfigMapper;
import com.show.business.integrate.service.IDyvmConfigService;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.show.business.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 平台管理-语音服务配置表 服务实现类
 * </p>
 *
 * @author dvlp
 * @since 2024-10-25
 */
@Service
public class DyvmConfigServiceImpl extends ServiceImpl<DyvmConfigMapper, DyvmConfig> implements IDyvmConfigService {

    @Override
    public List<DyvmConfig> getList(DyvmConfig deviceGroup) {
        return baseMapper.getList(deviceGroup);
    }

    @Override
    public AjaxResult saveInfo(DyvmConfig dyvmConfig) {
        if(StrUtil.hasBlank(dyvmConfig.getAccessKeyId(),dyvmConfig.getAccessKeySecret(),dyvmConfig.getTemplateCode(),dyvmConfig.getTemplateType())){
            return AjaxResult.error("必填项不能为空");
        }
        dyvmConfig.setDelFlag("0");
        dyvmConfig.setCreateTime(DateTime.now());
        dyvmConfig.setCreateBy(getUsername());
        baseMapper.insert(dyvmConfig);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult editInfo(DyvmConfig dyvmConfig) {
        if(StrUtil.hasBlank(dyvmConfig.getAccessKeyId(),dyvmConfig.getAccessKeySecret(),dyvmConfig.getTemplateCode(),dyvmConfig.getTemplateType(),dyvmConfig.getId())){
            return AjaxResult.error("必填项不能为空");
        }
        dyvmConfig.setUpdateTime(DateTime.now());
        dyvmConfig.setUpdateBy(getUsername());
        baseMapper.updateById(dyvmConfig);
        return AjaxResult.success();
    }

    /**
     * 启用设备分组
     * @param groupId
     * @return
     */
    @Override
    public AjaxResult enable(String groupId){
        DyvmConfig dyvmConfig=baseMapper.selectById(groupId);
        dyvmConfig.setStatus("1");
        baseMapper.updateById(dyvmConfig);
        return AjaxResult.success();
    }

    /**
     * 禁用设备分组
     * @param groupId
     * @return
     */
    @Override
    public AjaxResult disable(String groupId){
        DyvmConfig dyvmConfig=baseMapper.selectById(groupId);
        dyvmConfig.setStatus("0");
        baseMapper.updateById(dyvmConfig);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getInfo(String id) {
        return AjaxResult.success(baseMapper.getInfo(id));
    }

    @Override
    public AjaxResult delInfo(String id) {
        baseMapper.updateDel(id);
        return AjaxResult.success();
    }
    @Override
    public AjaxResult delInfos(String[] ids) {
        for(String id:ids){
            baseMapper.updateDel(id);
        }
        return AjaxResult.success();
    }

    @Override
    public DyvmConfig getConfig() {
        return baseMapper.getOneInfo();
    }

    @Override
    public DyvmConfig getInfoByType(String templateType) {
        return baseMapper.getInfoByType(templateType);
    }
}
